<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 我的关注
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['pluginset']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$goods_id=$_RQ['goods_id'];
	$act=pdo_get('jy_bargain_actor',array('goods_id'=>$goods_id,'uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid']));
	$info=pdo_get('jy_bargain_goods',array('id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']));
	$time=time()-$act['created_time'];
	$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	if(!$isfollow && $return['pluginset']['needfollow']){
		$return['showqrcode']=1;
	}
	if($act && (!$info['time_limit'] || $time< $info['time_limit']*3600)){
		exi('此商品您已有未结束的砍价，正在跳转','','/bargain/act/id='.$act['id']);
	}else{	
		if($info['maximum'] && $info['maximum']<= $info['initiate']){
			exi('该砍价商品已达到最大参与次数，请看看别的商品吧','error');
		}
		$goodid=pdo_getcolumn('jy_bargain_goods',array('id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']),'goods_id');
		$price=pdo_getcolumn('jy_weishop_goods',array('id'=>$goodid,'uniacid'=>$_SESSION['uniacid']),'price');
		pdo_insert('jy_bargain_actor',array('goods_id'=>$goods_id,'uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid'],'nickname'=>$_SESSION['member']['nickname'],'created_time'=>time(),'now_price'=>$price,'status'=>1));
		$aid=pdo_insertid();
		pdo_update('jy_bargain_goods',array('initiate +='=>1),array('id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']));		
		exi('','','/bargain/act/id='.$aid);
	}
	$return['sharedata']=array(
			'title'=>$info['title']?$info['title'].'-砍价':'砍价详情',
			'desc'=>$info['subtitle']?$info['subtitle']:'砍价详情-在线拼团商城！',
			'link'=>custom_url('jy_bargain/bargain//goods_id='.$goods_id.'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($info['image'])
	);
	set_html('title',$info['title']?:'砍价详情');
	exi($return);
}
if($_RQ['op']=='act'){
	$id=$_RQ['id'];
	if(!$id){
		exi('不存在砍价活动','error','/index/');
	}
	$basic=$this->plugin_setting('setting');
	$return['sharedata']=array(
		'title'=>$basic['title']?:'帮忙砍一下价！',
		'desc'=>$basic['desc'],
		'link'=>custom_url('jy_bargain/bargain/act/id='.$id,true),
		'imgUrl'=>ATTACHMENT_URL.$basic['image'],
	);
	$act=pdo_get('jy_bargain_actor',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	$time=time()-$act['created_time'];
	
	$act['created_time']=date('Y/m/d H:i:s',$act['created_time']);
	$good=pdo_get('jy_bargain_goods',array('id'=>$act['goods_id'],'uniacid'=>$_SESSION['uniacid']));
	if($time>$good['time_limit']*3600 && $good['time_limit']!=0){
		$act['time']='已结束';
	}else{
		$times=$good['time_limit']*3600-$time;
		 $hour = floor($times/3600);
         $minute = floor(($times-3600 * $hour)/60);
         $second = floor((($times-3600 * $hour) - 60 * $minute) % 60);
         //$act['time']= $hour.'小时'.$minute.'分'.$second.'秒';
		 $act['time']= $hour.'小时'.$minute.'分';
	}
	if(!$good['time_limit']){
		$act['time']='长期';
	}
	$good2=pdo_get('jy_weishop_goods',array('id'=>$good['goods_id'],'uniacid'=>$_SESSION['uniacid']),array('image','price','title','subtitle','total'));
	$good['image']=$good2['image'];
	$act['start_price']=$good2['price'];
	$act['avatar']=$_SESSION['member']['avatar'];
	$act['nickname']=$_SESSION['member']['nickname'];
	$act['user'] = model('core/member')->detail($_SESSION['uid'],array('nickname','avatar'));
	$good['title']=$good2['title'];
	$good['total']=$good2['total'];
	if($act['uid']==$_SESSION['uid']){
		$return['self']=1;
	}else{
		$return['self']=2;
	}
	$log=pdo_count('jy_bargain_record',array('uid'=>$_SESSION['uid'],'actor_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if($good['myself']==1 && $log<$good['each_time']){
		$return['myself']=1;
	}
	$swi=0;
	if($act['order']){
		$swi=1;
	}
	if($time>$good['time_limit']*3600 && $good['time_limit']!=0){
		$swi=2;
	}
	if($good2['total']<1){
		$swi=3;
	}
	if($good['show']!=1 && $act['now_price']!=$good['end_price']){
		$swi=4;
	}
	$return['swi']=$swi;
	$return['list']=pdo_getall('jy_bargain_record',array('actor_id'=>$id,'uniacid'=>$_SESSION['uniacid']),'','','bargain_time desc');
	foreach($return['list'] as &$v){
		$v['avatar']=pdo_getcolumn('core_members',array('id'=>$v['uid']),'avatar');
		$v['avatar']=$v['avatar']?$v['avatar']:'../core/resource/images/noavatar.gif';
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['uid']),'nickname');
		$v['nickname']=$v['nickname']?$v['nickname']:'匿名用户';
		$v['bargain_time']=date('Y/m/d H:i:s',$v['bargain_time']);
	}
	$act['goodsid']=$this->getcolumn('goods',array('id'=>$act['goods_id'],'uniacid'=>$_SESSION['uniacid']),'goods_id');
	$member=pdo_get('core_members',array('id'=>$act['uid'],'uniacid'=>$_SESSION['uniacid']),array('avatar','nickname'));
	$return['res']=$act;
	$return['member']=$member;
	if($good['maximum']!=0){
		$good['sy']=$good['maximum']-$good['initiate'];
	}else{
		$good['sy']='不限';
	}
	$mycanyu=pdo_get('jy_bargain_record',array('uid'=>$_SESSION['uid'],'actor_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['mycanyu']=$mycanyu['id']?1:0;
	$good['image']=tomedia($good['image']);
	$return['res2']=$good;
	$setting=$this->plugin_setting('basic');
	$return['rule']=$setting['rule'];
	set_html('title','参与砍价');
	exi($return);
}
if($_RQ['op']=='bargain'){
	$postdata=get_postdata();
	$actor_id=$postdata['actor_id'];
	$num=pdo_count('jy_bargain_record',array('uid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'actor_id'=>$actor_id));
	$act=pdo_get('jy_bargain_actor',array('id'=>$actor_id,'uniacid'=>$_SESSION['uniacid']));
	$good=pdo_get('jy_bargain_goods',array('id'=>$act['goods_id'],'uniacid'=>$_SESSION['uniacid']));
	$time=time()-$act['created_time'];
	$act['created_time']=date('Y/m/d H:i:s',$act['created_time']);
	if($time>$good['time_limit']*3600 && $good['time_limit']!=0){
		exi('砍价时间已结束','error');
	}
	
	
	if($act['now_price']<=$good['end_price']){
		exi('商品已到底价，无法再砍','error');
	}
	if($num>=$good['each_time']){
		exi('每人最多可砍价'.$good['each_time'].'次','error');
	}
	if($act['bargain_times']>=$good['total_time']){
		exi('已达到此商品最大砍价次数'.$good['total_time'].'次，不能继续砍价','error');
	}
	$p=json_decode($good['probability'],true);
	$num=rand(1,100);
	foreach($p as $k=>$v){
		
		if($num<=$v['jilv']){
			$bargain_price=round(rand($v['start']*100,$v['end']*100)/100,2);	
			break;
		}else{
			$num=$num-$v['jilv'];
		}
		
	}
	$up=array(
		'update_time'=>time(),
		'bargain_times +='=>1,
		
	);
	if($good['end_price']>($act['now_price']-$bargain_price)){
		$up['now_price']=$good['end_price'];
		$bargain_price=$act['now_price']-$good['end_price'];
		
		$data=array('picurl'=>$good['image'],'url'=>'jy_bargain/bargain/act/id='.$act['id'],'title'=>'您的砍价商品'.$good['title'].'有好友给您砍价，点击查看');
		cfc('account')->send_news($data,$act['uid']);
	}else{
		$up['now_price']=$act['now_price']-$bargain_price;
	}
	$up['bargain_price +=']=$bargain_price;
	$data=array(
		'actor_id'=>$actor_id,
		'bargain_time'=>time(),
		'uniacid'=>$_SESSION['uniacid'],
		'bargain_price'=>$bargain_price,
		'uid'=>$_SESSION['uid'],
		'nickname'=>$_SESSION['member']['nickname'],
	
	);
	pdo_insert('jy_bargain_record',$data);
	pdo_update('jy_bargain_actor',$up,array('id'=>$actor_id,'uniacid'=>$_SESSION['uniacid']));
	$return['money']=$bargain_price;
	$return['url']=custom_url('jy_bargain/bargain/act/id='.$act['id'],true);
	exi($return);
}